using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

using Autobase.WebControl.Managers;
using AutoBase.Shared;
using AutoBase.BusinessObjects;

namespace WebControl.VehicleSearch
{
    public partial class SearchBoxLong : System.Web.UI.UserControl
    {
        #region PROPERTIES

        /// <summary>
        /// Search By branch id
        /// </summary>
        //[Bindable(true)]
        //[Category("Data")]
        //[DefaultValue("")]
        //[Localizable(true)]
        public int BranchId
        {
            get
            {
                object o = this.ViewState["_BranchId"];
                if (o == null)
                {
#if (DEBUG)
                    return 2277;
#endif
                    return -1;
                }

                else
                {
                    return (int)o;
                }
            }

            set
            {
                this.ViewState["_BranchId"] = value;
            }
        }

        //[Bindable(true)]
        //[Category("Data")]
        //[DefaultValue("")]
        //[Localizable(true)]
        public string BranchIdLocationList
        {
            get
            {
                object o = this.ViewState["_BranchIdLocationList"];
                if (o == null)
                {
#if (DEBUG)
                    return "638|2264|2266";
#endif
                    //return string.Empty;
                }
                else
                    return (string)o;
            }
            set
            {
                this.ViewState["_BranchIdLocationList"] = value;
            }
        }

        //[Bindable(true)]
        //[Category("Data")]
        //[DefaultValue("")]
        //[Localizable(true)]
        public bool IsVisibleSearchButon
        {
            get
            {
                object o = this.ViewState["_IsVisibleSearchButon"];
                if (o == null)
                    return true;
                else
                    return (bool)o;
            }
            set
            {
                this.ViewState["_IsVisibleSearchButon"] = value;
            }
        }

        public int SelectedManufacturerId
        {
            get
            {
                object o = this.ViewState["_SelectedManufacturerId"];
                if (o == null)
                {
                    return -1;
                }
                else
                {
                    return (int)o;
                }
            }

            set
            {
                this.ViewState["_SelectedManufacturerId"] = value;
            }
        }

        public int SelectedModelId
        {
            get
            {
                object o = this.ViewState["_SelectedModelId"];
                if (o == null)
                {
                    return -1;
                }
                else
                {
                    return (int)o;
                }
            }
            set
            {
                this.ViewState["_SelectedModelId"] = value;
            }
        }

        public int SelectedYearTo
        {
            get
            {
                object o = this.ViewState["_SelectedYearTo"];
                if (o == null)
                {
                    return -1;
                }
                else
                {
                    return (int)o;
                }
            }
            set
            {
                this.ViewState["_SelectedYearTo"] = value;
            }
        }

        public int SelectedYearFrom
        {
            get
            {
                object o = this.ViewState["_SelectedYearFrom"];
                if (o == null)
                {
                    return -1;
                }
                else
                {
                    return (int)o;
                }
            }
            set
            {
                this.ViewState["_SelectedYearFrom"] = value;
            }
        }

        public decimal SelectedPriceFrom
        {
            get
            {
                object o = this.ViewState["_SelectedPriceFrom"];
                if (o == null)
                {
                    return -1;
                }
                else
                {
                    return (decimal)o;
                }
            }
            set
            {
                this.ViewState["_SelectedPriceFrom"] = value;
            }
        }

        public decimal SelectedPriceTo
        {
            get
            {
                object o = this.ViewState["_SelectedPriceTo"];
                if (o == null)
                {
                    return -1;
                }
                else
                {
                    return (decimal)o;
                }
            }
            set
            {
                this.ViewState["_SelectedPriceTo"] = value;
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadManufacturer();
            }
            RegisterClientScript();

        }


        protected void RegisterClientScript()
        {

        }

       protected void uicManufacturer_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadModels();
        }

        protected void uicSearch_Click(object sender, EventArgs e)
        {
            SelectedManufacturerId = (uicManufacturer.SelectedIndex > 0 ? Convert.ToInt32(uicManufacturer.SelectedValue) : -1);
            SelectedModelId = (uicModel.SelectedIndex > 0 ? Convert.ToInt32(uicModel.SelectedValue) : -1);
            SelectedPriceFrom = (uicPriceFrom.Text.Length > 0 ? Convert.ToDecimal(uicPriceFrom.Text) : -1);
            SelectedPriceTo = (uicPriceTo.Text.Length > 0 ? Convert.ToDecimal(uicPriceTo.Text) : -1);
            SelectedYearFrom = (uicYearFrom.Text.Length > 0 ? Convert.ToInt32(uicYearFrom.Text) : -1);
            SelectedYearTo = (uicYearTo.Text.Length > 0 ? Convert.ToInt32(uicYearTo.Text) : -1);

            SearchButtonClicked(sender, e);
        }

        protected void uicYearFrom_TextChanged(object sender, EventArgs e)
        {

        }


        private void LoadManufacturer()
        {
            List<string> BranchLocationList = new List<string>();
            
            //BranchLocationList.AddRange(new string[]{"638","2264","2266"});
            BranchLocationList.AddRange(this.BranchIdLocationList.Split('|'));

            uicManufacturer.DataSource = VehicleSearchManager.LoadManufacturer(BranchLocationList,2277);
            uicManufacturer.DataValueField = ViewDistinctManufacturerModelByBranchIdMetadata.ColumnNames.ManufacturerId;
            uicManufacturer.DataTextField = ViewDistinctManufacturerModelByBranchIdMetadata.ColumnNames.ManufacturerName;

            uicManufacturer.DataBind();

            uicManufacturer.Items.Insert(0, new ListItem("All Makes", "0"));

            uicModel.Items.Insert(0, new ListItem("All Models", "0"));
        }



        #region METHODS

        private void LoadModels()
        {
            if (Convert.ToInt64(Request.QueryString["id"]) > 0)
            {
                Response.Redirect(System.IO.Path.GetFileName(Request.CurrentExecutionFilePath));
            }

            List<string> BranchLocationList = new List<string>();

            //BranchLocationList.AddRange(new string[]{"638","2264","2266"});
            BranchLocationList.AddRange(this.BranchIdLocationList.Split('|'));

            if (BranchLocationList.Count> 0)
            {
                uicModel.DataSource = VehicleSearchManager.LoadModels(
                                                      Convert.ToInt32(uicManufacturer.SelectedValue),
                                                      BranchLocationList,
                                                      null);
                uicModel.DataValueField = ViewDistinctManufacturerModelByBranchIdMetadata.ColumnNames.ModelID;
                uicModel.DataTextField = ViewDistinctManufacturerModelByBranchIdMetadata.ColumnNames.ModelName;

                uicModel.DataBind();

                uicModel.Items.Insert(0, new ListItem("All Models", "0"));
            }
        }

        private void LoadSearchResults()
        {
            
        }

        public event EventHandler SearchButtonClicked;

        #endregion


    }
}